package store.lunangangster.engine.impl.bpmn.behavior;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import store.lunangangster.bpmn.model.StartEvent;
import store.lunangangster.bpmn.model.definition.MessageEventDefinition;
import store.lunangangster.engine.impl.Context;
import store.lunangangster.engine.impl.entity.ExecutionEntity;
import store.lunangangster.engine.impl.helper.MessageHelper;

public class StartMessageEventBehavior extends EventBehavior {

  private static final Logger log = LoggerFactory.getLogger(StartMessageEventBehavior.class);

  @Override
  public void doBehavior(ExecutionEntity execution) {
    StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
    MessageEventDefinition messageEventDefinition = (MessageEventDefinition) startEvent.getEventDefinitions().get(0);

    MessageHelper.listenToMessage(messageEventDefinition, execution);
  }

  @Override
  public void trigger(ExecutionEntity execution) {
    StartEvent startEvent = (StartEvent) execution.getCurrentFlowElement();
    Context.getProcessEngineConfiguration().getMesListenerEntityManager().deleteMesListenerByExecutionId(execution.getId());
    log.debug("{}监听到Message，被激活", startEvent);
  }
}
